
import React from "react";
import * as THREE from 'three';
import { RectAreaLightUniformsLib } from 'three/examples/jsm/lights/RectAreaLightUniformsLib';
import Controller from "../Controller";

const loader = new THREE.ObjectLoader();

/** 创建舞台 */
export default function useScene(controller: Controller, scene?: string | THREE.Scene) {

    /** canvas 容器 */
    const cont = controller.useMemo(s => s.cont, []);

    React.useEffect(() => {
        if (!cont) return;
        if (typeof scene === 'string') {
            loader.load(scene, function (scene) {
                controller.run(async s => {
                    s.scene = scene as any;
                })
            });
        } else if (scene) {
            controller.run(async s => {
                s.scene = scene;
            })
        } else {
            controller.run(async s => {
                s.scene = new THREE.Scene();
                s.scene.background = new THREE.Color(0x444444);
            })
        }
        RectAreaLightUniformsLib.init();
    }, [cont, controller, scene])
}